# coding= utf-8
import requests
import base64

# 获取assess_token(有效期30天)
# 引入三个参数
api_id = '15585176'
api_key = '5c2W4OLb6zLs0lX21pjqvE06'
secret_key = '61j8tsjw1YQ3b60mv3TQLZgsUb0PMAsc'
url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + str(api_key) + '&client_secret=' + str(secret_key)
res = requests.get(url).text
a = eval(res)
access_token = a['access_token']
print(access_token)

# 利用access_token做一些事情
animal = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/animal?access_token=' + str(access_token)

header = {
    'Content-Type' : 'application/x-www-form-urlencoded'
}
data = {}
with open('animal.jpg', 'rb') as f:
    image = base64.b64encode(f.read())
    # encode,decode 要学会熟练应用
    data["image"] = str(image).encode('utf-8')

    res2 = requests.post(url=animal,data=data, headers=header).text
    for each in eval(res2)['result']:
        print each['name'], '\t相似度：', each['score']
    # print 有无()  在py2 有着不一样的效果


# 这个接口的功能是会先框定动物的大致范围，然后进行相似度比较，1 为最大值，最大的就是正确



